unit ProbabilityEditorFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, Pawn, HourglassPawn, SkullPawn, JokerPawn,
  BonusPawn, ChameleonPawn, ColorsContainer, Buttons;

type
  TProbabilityEditorForm = class(TForm)
    bOK: TBitBtn;
    bCancel: TBitBtn;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    pbOverall: TProgressBar;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    iJoker: TImage;
    Label2: TLabel;
    tbJoker: TTrackBar;
    TabSheet2: TTabSheet;
    iBonus: TImage;
    Label6: TLabel;
    tbBonus: TTrackBar;
    TabSheet3: TTabSheet;
    iChameleon: TImage;
    Label4: TLabel;
    tbChameleon: TTrackBar;
    TabSheet4: TTabSheet;
    iHourglass: TImage;
    Label5: TLabel;
    tbHourglass: TTrackBar;
    TabSheet5: TTabSheet;
    iSkull: TImage;
    Label3: TLabel;
    tbSkull: TTrackBar;
    procedure FormCreate(Sender: TObject);
    procedure tbJokerChange(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
     JokerPrev: TJokerPawn;
     BonusPrev: TBonusPawn;
     ChameleonPrev: TChameleonPawn;
     HourglassPrev: THourglassPawn;
     SkullPrev: TSkullPawn;
     RefreshLocked: Boolean;

  public
    { Public declarations }
    procedure LockRefresh(Locked: Boolean);
    procedure RefreshPositions(newPos: Integer);
  end;

const
 ESC_KEYCODE = #27;

var
  ProbabilityEditorForm: TProbabilityEditorForm;

implementation

{$R *.dfm}

procedure TProbabilityEditorForm.LockRefresh(Locked: boolean);
begin
 RefreshLocked := Locked;
end;

procedure TProbabilityEditorForm.RefreshPositions(newPos: Integer);
begin
 tbJoker.Max := tbJoker.Position + newPos;
 tbBonus.Max := tbBonus.Position + newPos;
 tbSkull.Max := tbSkull.Position + newPos;
 tbChameleon.Max := tbChameleon.Position + newPos;
 tbHourglass.Max := tbHourglass.Position + newPos;
end;

procedure TProbabilityEditorForm.FormCreate(Sender: TObject);
begin
 JokerPrev := TJokerPawn.Create(iJoker.Width);
 BonusPrev := TBonusPawn.Create(iBonus.Width, clRed, 0);
 ChameleonPrev := TChameleonPawn.Create(iChameleon.Width, clRed, 0, TColorContainer.Create(1));
 HourglassPrev := THourglassPawn.Create(iHourglass.Width, clRed, 0);
 SkullPrev := TSkullPawn.Create(iSkull.Width, clRed, 0);

 JokerPrev.Draw(iJoker.Canvas);
 BonusPrev.Draw(iBonus.Canvas);
 ChameleonPrev.Draw(iChameleon.Canvas);
 HourglassPrev.Draw(iHourglass.Canvas);
 SkullPrev.Draw(iSkull.Canvas);
end;

procedure TProbabilityEditorForm.tbJokerChange(Sender: TObject);
var
 newPos: integer;
begin

 if (sender is TTrackBar) then
  begin
   newPos := 100 - tbJoker.Position - tbBonus.Position - tbChameleon.Position - tbSkull.Position - tbHourglass.Position;
   pbOverall.Position := newPos;
   if (not RefreshLocked) then RefreshPositions(newPos);
  end;
end;

procedure TProbabilityEditorForm.FormKeyPress(Sender: TObject;
  var Key: Char);
begin
 if Key = ESC_KEYCODE then ModalResult := mrCancel; 
end;

end.
